package com.accenture.fd.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.accenture.fd.entity.AuthorityEntity;
import com.accenture.fd.entity.SrcType;

@Mapper
public interface AuthorityDao {

	@Select("select screen_id from TB00016 where screen_id = #{screenId}")
	@Results({ @Result(property = "screenId", column = "screen_id"), })
	List<String> selFromAuthorityTable(@Param("screenId") String screenId);

	@Delete("delete from TB00016 where screen_id = #{screenId}")
	void delFromAuthorityTable(@Param("screenId") String screenId);

	@Insert({ "<script>", "insert into TB00016 value ",
			"<foreach collection='entityList' item='entity' index='index' separator=','>",
			"(#{entity.screenId},#{entity.section},#{entity.authorityId},#{entity.authorityNm},#{entity.authorityValue})",
			"</foreach>", "</script>" })
	void saveAuthorityEntityList(@Param(value = "entityList") List<AuthorityEntity> authorityEntityList);
	
    @Select("select distinct section \r\n" +
     		"  from df.tb00016 tb1 \r\n" +
     		"  where screen_id = #{screenId} \r\n" +
     		"  and (not exists(select 'x' from df.tb00016  tb2 \r\n" +
     		"                 where (tb2.authority_value='〇'  or tb2.authority_value='●' or tb2.authority_value='-') \r\n" +
     		"                 and tb1.section=tb2.section and tb1.screen_id=tb2.screen_id) \r\n" +
     		"    or not exists(select 'x' from df.tb00016  tb3 \r\n" +
     		"     		      where (tb3.authority_value='〇'  or tb3.authority_value='×' or tb3.authority_value='-')  \r\n" +
     		"                 and tb1.section=tb3.section and tb1.screen_id=tb3.screen_id))")
    @Results({@Result(property = "section", column = "section")})
	List<String> selForUnauthorityItem(@Param("screenId") String screenId);
}